﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Pai.LiveStation.ConsolePlayer
{
    public class MediaFile
    {
        [DllImport("winmm.dll")]
        static extern int mciSendString(string mciCommand, StringBuilder buffer, int bufferSize, IntPtr callback);
        string fileName;
        private StringBuilder sBuffer = new StringBuilder(128);
        private int seconds;
        private int ReturnSeconds;

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetShortPathName(
            [MarshalAs(UnmanagedType.LPTStr)]
        string path,
            [MarshalAs(UnmanagedType.LPTStr)]
        StringBuilder shortPath,
            int shortPathLength
            );

        public void Send(string mciCommand)
        {
            mciSendString(mciCommand, null, 0, IntPtr.Zero);
        }

        public MediaFile(string fileLocation)
        {
            StringBuilder shortPath = new StringBuilder(255);
            int retVal = GetShortPathName(fileLocation, shortPath, shortPath.Capacity);

            string shortFile = shortPath.ToString().Substring(0, retVal);

            fileName = shortFile;
            Send("close");
            string sCommand = String.Format(@"open ""{0}""", fileName);
            Send(sCommand);
        }
        public void Play()
        {
            Send("play " + fileName);
        }
        public int Position
        {
            set
            {
                string sCommand = "";
                seconds = value;
                seconds = seconds * 1000;
                sCommand = "play sFile from " + seconds.ToString();
                mciSendString(sCommand, null, 0, IntPtr.Zero);
            }

            get
            {
                string sCommand = "";
                sCommand = "status " + fileName + " position";
                mciSendString(sCommand, sBuffer, sBuffer.Capacity, IntPtr.Zero);

                if (sBuffer.Length > 0)
                {
                    seconds = int.Parse(sBuffer.ToString());
                    seconds = seconds / 1000;
                    return seconds;
                }
                return 0;
            }
        }

        public string CurrentMedia()
        {
            throw new NotImplementedException();
        }

        public bool CurrentMediaPlaying()
        {
            string sCommand = "";
            sCommand = "status " + fileName + " mode";
            mciSendString(sCommand, sBuffer, 128, IntPtr.Zero);

            if (sBuffer.Length > 0)
            {
                string blah = sBuffer.ToString();
                //ReturnSeconds = int.Parse(sBuffer.ToString());
                //ReturnSeconds = ReturnSeconds / 1000;

                if (blah.Equals("playing"))
                    return true;
            }
            return false;
        }

        public int Duration()
        {
            string sCommand = "";
            sCommand = "status " + fileName + " length";
            mciSendString(sCommand, sBuffer, 128, IntPtr.Zero);

            if (sBuffer.Length > 0)
            {
                ReturnSeconds = int.Parse(sBuffer.ToString());
                ReturnSeconds = ReturnSeconds / 1000;
                return ReturnSeconds;
            }
            return 0;
        }
    }
}
